﻿using RpcCacheClient.Interface;
using RpcCentral.Common;
using RpcCentral.DAL;
using RpcCentral.Model.DB;
using RpcModel;

namespace RpcCentral.Collect.Collect
{
    internal class ReduceInRankCollect : IReduceInRankCollect
    {
        private readonly IReduceInRankConfigDAL _ReduceInRankConfig;
        private readonly ICacheController _Cache;
        public ReduceInRankCollect(IReduceInRankConfigDAL reduceInRankConfig, ICacheController cache)
        {
            _Cache = cache;
            _ReduceInRankConfig = reduceInRankConfig;
        }

        public ReduceInRank GetReduceInRank(long rpcMerId, long serverId)
        {
            string key = string.Format("Reduce_{0}_{1}", rpcMerId, serverId);
            if (this._Cache.TryGet(key, out ReduceInRank config))
            {
                return config;
            }
            ReduceInRankConfig data = _ReduceInRankConfig.GetReduceInRank(rpcMerId, serverId);
            if (data == null)
            {
                config = new ReduceInRank { IsEnable = false, FusingErrorNum = 1 };
            }
            else
            {
                config = data.ConvertMap<ReduceInRankConfig, ReduceInRank>();
            }
            this._Cache.Set(key, config, new TimeSpan(10, 0, 0, 0));
            return config;
        }

        public void Refresh(long rpcMerId, long serverId)
        {
            string key = string.Format("Reduce_{0}_{1}", rpcMerId, serverId);
            this._Cache.Remove(key);
        }
    }
}
